﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SHack.Helper;
using System.Threading;

namespace SHack.WorkSpace
{
    class HttpScan:BaseScan
    {
        private string _method;
        private string _url;
        private string _data;
        private string _header;
        private Encoding _encoding;

        private bool _isurlvar;
        private bool _isdatavar;
        private bool _isheadervar;
        private bool _istimestamp;

        private string[] _htInclude;
        private string[] _htUnclude;
        private bool _isInclude;


        private bool _isstopauto;
        private VarFile _varFile;
        private object locker = new object();

        public delegate void FoundVarHandler(string url, string data, string header);
        public event FoundVarHandler FoundVarEvent;

        public bool Start(VarFile varFile, bool isstopauto,int threadCount, int timeOut, string method,string url, string data, string header, Encoding encoding,bool isInclude,string[] htInclude,string[] htUnclude)
        {
            _method = method;
            _url = url;
            _data = data;
            _header = header;
            _encoding = encoding;
            _htInclude = htInclude;
            _htUnclude = htUnclude;
            _isInclude = isInclude;

            HttpSocketHelper.TimeOut = timeOut;

            _varFile = varFile.Clone();
            _isdatavar = _varFile.IsVar(data);
            _isheadervar = _varFile.IsVar(header);
            _isurlvar = _varFile.IsVar(url);
          
            _isstopauto = isstopauto;

      
            _totalCount = _varFile.TotalCount;
            _passCount = 0;
            _error = "";
            _lastTime = DateTime.Now;
            _isrun = true;

            for (int i = 0; i < threadCount; i++)
            {
                new Thread(thScan).Start();
            }
            
            return true;
        }
        public bool Stop()
        {
            _isrun = false;
            return true;
        }
        private void thScan()
        {

            Hashtable htVal = new Hashtable();
            SortedList<string, string> sl;
            while (_isrun)
            {
                lock (locker)
                {
                    sl = _varFile.Get();
                    if (sl == null)
                    {
                        break;
                    }
                }



                string url = _isurlvar ? _varFile.Get(_url, sl) : _url;
                string data = _isdatavar ? _varFile.Get(_data, sl) : _data;
                string header = _isheadervar ? _varFile.Get(_header, sl) : _header;

                string ret = HttpSocketHelper.Get(url, data, _header, _encoding);
                bool isFound = _isInclude ? IsInclude(ret) : IsUnclude(ret);

                AddPass();

                if (isFound)
                {
                    AddSucc();
                    if (FoundVarEvent != null)
                        FoundVarEvent(url, data, header);

                    if (_isstopauto)
                        break;
                }
            }

            CallStop();
        }

        private bool IsInclude(string ret)
        {
            foreach (string r in _htInclude)
            {
                if (ret.Contains(r))
                    return true;
            }
            return false;
        }


        private bool IsUnclude(string ret)
        {
            foreach (string r in _htUnclude)
            {
                if (ret.Contains(r))
                    return false;
            }
            return true;
        }

    }
}
